# gozerlib/admin.py
#
#

""" admin related data and functions. """

## gozerlib imports

from gozerlib.persist.persist import Persist
from gozerlib.commands import cmnds
from gozerlib.aliases import aliasreverse

## basic imports

import logging

plugin_packages = ['gozerlib.plugs', 'waveplugs']
default_plugins = ['gozerlib.plugs.admin', 'gozerlib.plugs.outputcache', 'waveplugs.watcher', 'waveplugs.hubbub', 'waveplugs.relay', 'waveplugs.wave']

def savecmndtable():

    """ save command -> plugin list to db backend. """

    cmndtable = Persist('cmndtable')
    cmndtable.data = {}

    for cmndname, c in cmnds.store.iteritems():
        cmndtable.data[cmndname] = c.modname   
        a = aliasreverse(cmndname)
        if a:
            cmndtable.data[a] = c.modname

    logging.warn("saving command table")
    cmndtable.save()

def cmndtable():

    """ save command -> plugin list to db backend. """

    cmndtable = Persist('cmndtable')
    return cmndtable.data 

def savepluginlist():

    """ save a list of available plugins to db backend. """

    pluginlist = Persist('pluginlist')
    pluginlist.data = []

    for cmndname, c in cmnds.store.iteritems():
        if c.plugname not in pluginlist.data:
            pluginlist.data.append(c.plugname)

    pluginlist.data.sort()
    logging.warn("saving plugin list")
    pluginlist.save()

def pluginlist():

    """ get the plugin list. """

    pluginlist = Persist('pluginlist')
    return pluginlist.data
